/**
 * Copyright (c) 华南农业大学信息学院蔡超敏2015版权所有
 * 
 * 文件创建时间：2015年4月6日
 */
package org.scauinfo.fpos.action.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.scauinfo.fpos.action.BaseAction;
import org.scauinfo.fpos.service.ProductTypeService;
import org.scauinfo.fpos.util.ExceptionUtil;
import org.scauinfo.fpos.util.Json;
import org.scauinfo.fpos.vo.ProductType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

/**
 * @author 蔡超敏
 * 
 */
@Namespace("/")
@Scope("prototype")
@Controller
@Action(value = "productType", results = { @Result(name = "productType", location = "/productType.jsp") })
public class ProductTypeAction extends BaseAction implements
		ModelDriven<ProductType> {

	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger
			.getLogger(ProductTypeAction.class);
	@Autowired
	private ProductTypeService productTypeService;
	public ProductType productType = new ProductType();

	/**
	 * 得到数据列表
	 */
	public void grid() {
		System.out.println("productType-tags ----1");
		int total = productTypeService.getTotal();
		System.out.println("tags ----total"+total+"page"+page+"rows"+rows+", sort"+sort+", order"+order);
		List<ProductType> list = productTypeService.findAllProductType(page,
				rows, sort, order);
		System.out.println("productTypetags ----2"+list.size());
		for (ProductType productType : list) {
			productType.setProducts(null);
		}
		writeJson(total, list);
		System.out.println("tags ----3");
	}
	
	/**
	 * 得到数据列表
	 */
	public void combogrid() {
		System.out.println("productType-tags ----1");
		int total = productTypeService.getTotal();
		System.out.println("tags ----total"+total+"page"+page+"rows"+rows+", sort"+sort+", order"+order);
		List<ProductType> list = productTypeService.findAllProductType();
		System.out.println("tags ----2"+list.size());
		for (ProductType productType : list) {
			productType.setProducts(null);
		}
		writeJson(total, list);
		System.out.println("tags ----3");
	}

	/**
	 * 添加操作
	 */
	public void add() {
		Json result = new Json();
		try {
			productTypeService.saveProductType(productType);
			result.setSuccess(true);
			result.setMsg("添加产品类别成功");
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("添加产品类别失败");
		}
		writeJson(result);
	}

	/**
	 * 修改操作
	 */
	public void edit() {
		Json result = new Json();
		try {
			productTypeService.updateProductType(productType);
			result.setSuccess(true);
			result.setMsg("修改产品类别成功");
			result.setSuccess(true);
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("添加产品类别失败");
		}
		writeJson(result);
	}

	/**
	 * 删除操作
	 */
	public void remove() {
		Json result = new Json();
		try {
			System.out.println("tags ----="+productType.getId());
			productTypeService.deleteProductType(productType.getId());
			result.setSuccess(true);
			result.setMsg("删除产品类别成功");
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("删除产品类别失败");
		}
		writeJson(result);
	}

	@Override
	public ProductType getModel() {
		return productType;
	}
}